from django.shortcuts import render, redirect, reverse
from django.views.generic import View
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout, get_user_model
from django.contrib.auth.decorators import login_required
from .models import User
from .forms import LoginForm


# TODO 3.继承自AbstractUser
# class IndexView(View):
#     def get(self, request, *args, **kwargs):
#         telphone = '18788888887'
#         username = 'zhiliao1@qq.com'
#         password = '18788888887'
#         # TODO: 创建用户
#         # User.objects.create_user(telphone=telphone, username=username, password=password)
#         # TODO: 创建管理员
#         # User.objects.create_superuser(telphone=telphone, username=username, password=password)
#
#         # TODO: 登录验证
#         user = authenticate(request, telphone=telphone, password=password)
#         if user:
#             print('当前登录用户：%s' % user.username)
#         else:
#             print('用户名或密码错误')
#
#         return HttpResponse('success')


# TODO 4.继承自AbstractBaseUser模型
class IndexView(View):
    def get(self, request, *args, **kwargs):
        telphone = '18877779999'
        username = '18877779999@qq.com'
        password = '18877779999'
        # TODO: 创建用户
        # User.objects.create_user(telphone=telphone, username=username, email=username, password=password)
        # TODO: 创建管理员
        # User.objects.create_superuser(telphone=telphone, username=username, email=username, password=password)

        # TODO：登录验证
        # user = authenticate(telphone=telphone, password=password)
        # if user:
        #     print('当前登录用户：%s' % user.username)
        # else:
        #     print('用户名或密码错误')

        get_user_model().objects.create_user(telphone=telphone, username=username, password=password, email=username)
        return HttpResponse('success')


class LoginView(View):
    def get(self, request, *args, **kwargs):
        return render(request, 'login.html')

    def post(self, request, *args, **kwargs):
        form = LoginForm(request.POST)
        if form.is_valid():
            telphone = form.cleaned_data.get('telphone')
            password = form.cleaned_data.get('password')
            remember = form.cleaned_data.get('remember')
            user = authenticate(request, telphone=telphone, password=password)
            # TODO: 账户存在且可用
            if user and user.is_active:
                login(request, user)
                if remember:
                    # TODO: 记住我 使用全局的session配置 默认2周有效时间
                    request.session.set_expiry(None)
                else:
                    # TODO: 浏览器关闭session就会过期
                    request.session.set_expiry(0)
                next_url = request.GET.get('next')
                if next_url:
                    # TODO: 重定向
                    return redirect(next_url)
                else:
                    return HttpResponse('登录成功')
            else:
                print('用户名或者密码错误')
                # TODO 登录失败重定向到当前页面
                return redirect(reverse('login'))
        else:
            print(form.errors.get_json_data())
            # TODO 登录失败重定向到当前页面
            return redirect(reverse('login'))


class LogoutView(View):
    def get(self, request, *args, **kwargs):
        logout(request)
        return HttpResponse('退出登录成功')


# @login_required(login_url='/login/')
# class ProfileView(View):
#     def get(self, request, *args, **kwargs):
#         return render(request, 'profile.html')

@login_required(login_url='/login/')
def ProfileView(request):
    return render(request, 'profile.html')
